<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    canvas {
      border: 1px solid #ccc;
    }
  </style>
</head>
<body>
  <canvas class="canvas" width="500" height="500"></canvas>
  <!-- 顶点着色器 -->
  <script id="vertex-shader" type="x-shader/x-vertex">
    attribute vec4 aPos;
    void main() {
      float radian = radians(10.0);
      float cos = cos(radian);
      float sin = sin(radian);
      mat4 mx = mat4(
        1, 0, 0, 0,
        0, cos, -sin, 0,
        0, sin, cos, 0,
        0, 0, 0, 1
      );
      mat4 my = mat4(
        cos, 0, -sin, 0,
        0, 1, 0, 0,
        sin, 0, cos, 0,
        0, 0, 0, 1
      );
      gl_Position = mx * my * aPos;
      gl_PointSize = 10.0;
    }
  </script>
  <!-- 片段着色器 -->
  <script id="fragment-shader" type="x-shader/x-fragment">
    precision mediump float;
    uniform vec4 uColor;
    void main() {
      gl_FragColor = uColor;
    } 
  </script>
  <script type="module">
    import initShader from "./utils/initShader.js";
    const canvas = document.querySelector(".canvas");
    const gl = canvas.getContext("webgl");
    const program = initShader(gl);

    /*
      [
        // 前面
        -0.5, -0.5, 0.5,
        0.5, -0.5, 0.5,
        0.5, 0.5, 0.5,
        -0.5, 0.5, 0.5,
        // 后面
        -0.5, -0.5, -0.5,
        -0.5, 0.5, -0.5,
        0.5, 0.5, -0.5,
        0.5, -0.5, -0.5,
        // 上面
        -0.5, 0.5, -0.5,
        -0.5, 0.5, 0.5,
        0.5, 0.5, 0.5,
        0.5, 0.5, -0.5,
        // 下面 
        -0.5, -0.5, -0.5,
        0.5, -0.5, -0.5,
        0.5, -0.5, 0.5,
        -0.5, -0.5, 0.5,
        // 右面
        0.5, -0.5, -0.5,
        0.5, 0.5, -0.5,
        0.5, 0.5, 0.5,
        0.5, -0.5, 0.5
        // 左面
        -0.5, -0.5, -0.5,
        -0.5, -0.5, 0.5,
        -0.5, 0.5, 0.5,
        -0.5, 0.5, -0.5,
      ]
    */
    const vertices = new Float32Array([        
      // 前面
      -0.5, -0.5, 0.5,
      0.5, -0.5, 0.5,
      0.5, 0.5, 0.5,
      -0.5, 0.5, 0.5,
      // 后面
      -0.5, -0.5, -0.5,
      -0.5, 0.5, -0.5,
      0.5, 0.5, -0.5,
      0.5, -0.5, -0.5,
      // 上面
      -0.5, 0.5, -0.5,
      -0.5, 0.5, 0.5,
      0.5, 0.5, 0.5,
      0.5, 0.5, -0.5,
      // 下面 
      -0.5, -0.5, -0.5,
      0.5, -0.5, -0.5,
      0.5, -0.5, 0.5,
      -0.5, -0.5, 0.5,
      // 右面
      0.5, -0.5, -0.5,
      0.5, 0.5, -0.5,
      0.5, 0.5, 0.5,
      0.5, -0.5, 0.5
      // 左面
      -0.5, -0.5, -0.5,
      -0.5, -0.5, 0.5,
      -0.5, 0.5, 0.5,
      -0.5, 0.5, -0.5,
    ])

    // 缓冲区
    const buffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

    const indices = new Uint16Array([
      0, 1, 2, 0, 2, 3,
      4, 5, 6, 4, 6, 7,
      8, 9, 10, 8, 10, 11,
      12, 13, 14, 12, 14, 15,
      16, 17, 18, 16, 18, 19,
      20, 21, 22, 20, 22, 23
    ])

    const indexBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

    const posLocation = gl.getAttribLocation(program, "aPos");

    gl.vertexAttribPointer(
      posLocation,
      3,
      gl.FLOAT,
      false,
      3 * Float32Array.BYTES_PER_ELEMENT,
      0
    )
    gl.enableVertexAttribArray(posLocation);

    const faceColor = [
      [1.0, 0.0, 0.0, 1.0], // 前面: 红色
      [0.0, 1.0, 0.0, 1.0], // 后面: 绿色
      [0.0, 0.0, 1.0, 1.0], // 上面: 蓝色
      [1.0, 1.0, 0.0, 1.0], // 下面: 黄色
      [1.0, 0.0, 1.0, 1.0], // 右边: 洋红色
      [0.0, 1.0, 1.0, 1.0], // 左边: 蓝绿色
    ];

    const uColorLocation = gl.getUniformLocation(program, "uColor");

    for (let i = 0; i < 6; i++) {
      console.log(i * 6 * Uint16Array.BYTES_PER_ELEMENT);
      gl.uniform4fv(uColorLocation, faceColor[i]);
      gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, i * 6 * Uint16Array.BYTES_PER_ELEMENT);
    }
  </script>
</body>
</html>